package com.puhui.education.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.puhui.education.R;
import com.puhui.education.activity.base.BaseAcy;
import com.puhui.education.adapter.ViewHolder;
import com.puhui.education.adapter.base.recyclerview.CommonAdapter;
import com.puhui.education.bean.CourseMoreInfo;
import com.puhui.education.util.DynamicLayoutParamsUtil;
import com.puhui.education.util.ToastUtil;
import com.puhui.education.view.SwipeItemLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/15.
 */
public class MyCollectionAcy extends BaseAcy {
    @Bind(R.id.rv_courseInfo_myCollectionAcy)
    RecyclerView rv_courseInfo;
    private List<CourseMoreInfo> collectCourseList;
    private Activity activity;
    private List<SwipeItemLayout> swipeItemLayoutList;
    private List<SwipeItemLayout> swipeItemLayoutOpenedList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acy_my_collection);
        ButterKnife.bind(this,this);
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initData() {
        super.initData();
        activity = MyCollectionAcy.this;
        swipeItemLayoutList = new ArrayList<>();
        swipeItemLayoutOpenedList = new ArrayList<>();
        initCollectCourseInfo();
        rv_courseInfo.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        rv_courseInfo.setAdapter(new CommonAdapter<CourseMoreInfo>(activity,R.layout.item_rv_collect_course,collectCourseList) {
            @Override
            public void convert(final ViewHolder holder, final CourseMoreInfo courseMoreInfo) {
                final SwipeItemLayout swipeItemLayout = holder.getView(R.id.sil_itemRvCollectCourse);
                if(swipeItemLayout.isOpened()){
                    swipeItemLayout.close();
                }
                holder.setText(R.id.tv_price_itemRvCollectCourse, "¥" + courseMoreInfo.getPrice());
                holder.setOnClickListener(R.id.tv_deleteItem_itemRvCollectCourse, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closeAllSwipeItemLayout();
                        swipeItemLayoutList.remove(swipeItemLayout);
                        collectCourseList.remove(holder.getAdapterPosition());
                        notifyItemRemoved(holder.getAdapterPosition());

                    }
                });
                holder.setOnClickListener(R.id.sil_itemRvCollectCourse, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.showBottomToast("价格：¥" + courseMoreInfo.getPrice());
                    }
                });
                if(cacheSwipeItemLayout(swipeItemLayout)){
                    swipeItemLayout.setLayoutParams
                            (DynamicLayoutParamsUtil.createViewGroupLayoutParams(DynamicLayoutParamsUtil.WIDTH_MATCH, -1f, 0.22f));
                    swipeItemLayout.setDelegate(new SwipeItemLayout.SwipeItemLayoutDelegate() {
                        @Override
                        public void onSwipeItemLayoutOpened(SwipeItemLayout swipeItemLayout) {
                            exchangeSwipeItemLayout(swipeItemLayout);
                        }

                        @Override
                        public void onSwipeItemLayoutClosed(SwipeItemLayout swipeItemLayout) {
                        }

                        @Override
                        public void onSwipeItemLayoutStartOpen(SwipeItemLayout swipeItemLayout) {
                            exchangeSwipeItemLayout(swipeItemLayout);
                        }
                    });

                }
            }
        });

    }

    private void initCollectCourseInfo() {
        collectCourseList = new ArrayList<>();
        CourseMoreInfo info1 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 1f, 200,1);
        CourseMoreInfo info2 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 2f, 200, 2);
        CourseMoreInfo info3 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 3f, 200, 3);
        CourseMoreInfo info4 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 4f, 200, 4);
        CourseMoreInfo info5 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 5f, 200, 5);
        CourseMoreInfo info6 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 6f, 200, 6);
        CourseMoreInfo info7 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 7f, 200, 7);
        CourseMoreInfo info8 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 8f, 200, 8);
        CourseMoreInfo info9 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 9f, 200, 9);
        CourseMoreInfo info10 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 10f, 200, 10);
        CourseMoreInfo info11 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 11f, 200,11);
        CourseMoreInfo info12 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 12f, 200, 12);
        CourseMoreInfo info13 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 13f, 200, 13);
        CourseMoreInfo info14 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 14f, 200, 4);
        CourseMoreInfo info15 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 15f, 200, 5);
        CourseMoreInfo info16 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 16f, 200, 6);
        CourseMoreInfo info17 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 17f, 200, 7);
        CourseMoreInfo info18 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 18f, 200, 8);
        CourseMoreInfo info19 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 19f, 200, 9);
        CourseMoreInfo info20 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 20f, 200, 10);
        collectCourseList.add(info1);
        collectCourseList.add(info2);
        collectCourseList.add(info3);
        collectCourseList.add(info4);
        collectCourseList.add(info5);
        collectCourseList.add(info6);
        collectCourseList.add(info7);
        collectCourseList.add(info8);
        collectCourseList.add(info9);
        collectCourseList.add(info10);
        collectCourseList.add(info11);
        collectCourseList.add(info12);
        collectCourseList.add(info13);
        collectCourseList.add(info14);
        collectCourseList.add(info15);
        collectCourseList.add(info16);
        collectCourseList.add(info17);
        collectCourseList.add(info18);
        collectCourseList.add(info19);
        collectCourseList.add(info20);
    }

    //关闭所有展开的布局
    private void closeAllSwipeItemLayout(){
        for (SwipeItemLayout s : swipeItemLayoutList) {
            if ( s.isOpened()) {
                s.close();
            }
        }
    }
    //转换显示的Item，保证只显示一个
    private void exchangeSwipeItemLayout(SwipeItemLayout swipeItemLayout){
        for (SwipeItemLayout s : swipeItemLayoutList) {
            if ( s!= swipeItemLayout) {
                if(s.isOpened()){
                    s.closeWithAnim();
                }
            }else{
                swipeItemLayoutOpenedList.add(s);
            }
        }
        for (int i = (swipeItemLayoutOpenedList.size()-1); i >0; i--) {
            if(swipeItemLayoutOpenedList.get(i).isOpened()){
                swipeItemLayoutOpenedList.get(i).closeWithAnim();
            }
        }
        swipeItemLayoutOpenedList.clear();
    }
    //缓存Item布局
    private boolean cacheSwipeItemLayout(SwipeItemLayout swipeItemLayout) {
        boolean isAddEnable = true;
        if (swipeItemLayoutList.size() != 0) {
            for (SwipeItemLayout s : swipeItemLayoutList) {
                if (swipeItemLayout == s) {
                    isAddEnable = false;
                }
            }
        } else {
            swipeItemLayoutList.add(swipeItemLayout);
            return isAddEnable;
        }
        if (isAddEnable) {
            swipeItemLayoutList.add(swipeItemLayout);
        }
        return isAddEnable;
    }
}
